/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TarefaView.java
 *
 * Created on 04/11/2009, 17:05:12
 */

package view.execucao;

import Util.manipulaConexao;

import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import model.execucao.ProcAtivDao;
import model.processo.Processo;
import model.processo.ProcessoData;
import model.sistema.Grupo;
import model.sistema.Pacote;
import model.sistema.PacoteData;
import view.sistema.Principal;

/**
 *
 * @author Administrador
 */
public class TarefaView extends javax.swing.JDialog {

    /** Creates new form TarefaView */
    public TarefaView(java.awt.Frame parent, boolean modal, AgendaView agenda) {
        super(parent, modal);
        initComponents();
        this.agenda = agenda;
        montaPacotes();
        jtbpacotes.getSelectionModel().addListSelectionListener(selectionListener);
        jtbpacotes.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jtbprocessos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbpacotes = new javax.swing.JTable();
        jtpesquisarpac = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtbprocessos = new javax.swing.JTable();
        jtpesquisarproc = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jbok = new javax.swing.JButton();
        jbcancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tarefas");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Pacotes disponíveis", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jtbpacotes.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jtbpacotes);

        jtpesquisarpac.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jtpesquisarpacFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jtpesquisarpacFocusLost(evt);
            }
        });
        jtpesquisarpac.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jtpesquisarpacKeyReleased(evt);
            }
        });

        jLabel1.setText("Pesquisar :");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtpesquisarpac, javax.swing.GroupLayout.PREFERRED_SIZE, 362, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(333, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 752, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtpesquisarpac, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Processos", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N
        jPanel2.setFont(new java.awt.Font("Tahoma", 1, 11));

        jtbprocessos.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(jtbprocessos);

        jtpesquisarproc.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jtpesquisarprocKeyReleased(evt);
            }
        });

        jLabel2.setText("Pesquisar :");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtpesquisarproc, javax.swing.GroupLayout.PREFERRED_SIZE, 362, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(333, Short.MAX_VALUE))
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 752, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtpesquisarproc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jbok.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/header_complete.gif"))); // NOI18N
        jbok.setText("OK");
        jbok.setPreferredSize(new java.awt.Dimension(95, 25));
        jbok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbokActionPerformed(evt);
            }
        });

        jbcancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/delete_obj.gif"))); // NOI18N
        jbcancelar.setText("Cancelar");
        jbcancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbcancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbok, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(12, 12, 12)
                        .addComponent(jbcancelar))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap(10, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbok, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbcancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-800)/2, (screenSize.height-600)/2, 800, 600);
    }// </editor-fold>//GEN-END:initComponents

    private void montaPacotes()
    {
        Grupo grupo;
        ArrayList<Grupo> grupos = manipulaConexao.getGrupoLogado();
        String sql = "";
        for(int i = 0; i < grupos.size(); i++)
        {
            grupo = grupos.get(i);
            if(i == 0)
            {
                sql = "where b.idgrupo = "+ grupo.getId();

            }
            else
            {
                sql = sql + " or b.idgrupo = "+ grupo.getId();

            }
        }
        preencheTable(jtbpacotes, "select distinct a.idpacote as 'Código Pacote' , a.descricao as 'Descrição' from pacote a inner join grupo_pacote b on a.idpacote = b.idpacote " +sql);
    }

    private void jbcancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbcancelarActionPerformed
        dispose();
    }//GEN-LAST:event_jbcancelarActionPerformed

    private void jtpesquisarpacKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtpesquisarpacKeyReleased
        TableRowSorter<TableModel> sorter;
        sorter = new TableRowSorter<TableModel>(jtbpacotes.getModel());
        jtbpacotes.setRowSorter(sorter);
        sorter.setRowFilter(javax.swing.RowFilter.regexFilter(jtpesquisarpac.getText(),1));
        jtpesquisarproc.setText("");
        jtbpacotes.valueChanged(null);
        
    }//GEN-LAST:event_jtpesquisarpacKeyReleased

    private void jtpesquisarprocKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtpesquisarprocKeyReleased

        TableRowSorter<TableModel> sorter;
        sorter = new TableRowSorter<TableModel>(jtbprocessos.getModel());
        jtbprocessos.setRowSorter(sorter);
        sorter.setRowFilter(javax.swing.RowFilter.regexFilter(jtpesquisarproc.getText(),1));

    }//GEN-LAST:event_jtpesquisarprocKeyReleased

    private void jtpesquisarpacFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jtpesquisarpacFocusGained
        jtbprocessos.setEnabled(false);
    }//GEN-LAST:event_jtpesquisarpacFocusGained

    private void jtpesquisarpacFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jtpesquisarpacFocusLost
        jtbprocessos.setEnabled(true);
    }//GEN-LAST:event_jtpesquisarpacFocusLost

    private void jbokActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbokActionPerformed

    int linha = jtbprocessos.getSelectedRow();
            if(linha == -1)
            {
                JOptionPane.showMessageDialog(null, "Nenhum processo selecionado, verifique!","Erro",0);
                return;
            }
            Processo processo;
            try{
                processo = new ProcessoData().getProcessoId(Integer.valueOf(String.valueOf(jtbprocessos.getValueAt(linha, 0))));
                
                new ProcAtivDao().estanciaTarefasIniciais1(processo.getIdproceso(),0,0,0,desc);
                dispose();
                //agenda.tarefas();

                }catch(SQLException sql)
                {
                    JOptionPane.showMessageDialog(null, sql.getMessage());
                    sql.printStackTrace();
                }

            agenda.jbtatualiza.doClick();
    }//GEN-LAST:event_jbokActionPerformed

    private void preencheTable(JTable table, String sql)
    {
        manipulaConexao.preenchetable(table, sql);
    }

    private ListSelectionListener selectionListener = new ListSelectionListener()
    {
        public void valueChanged(ListSelectionEvent e)
        {
            int linha = jtbpacotes.getSelectedRow();
            if(linha == -1)
            {
                jtbprocessos.setModel(new DefaultTableModel());
                return;
            }
            Pacote pacote;
            pacote = new PacoteData().getPacoteId(Integer.valueOf(String.valueOf(jtbpacotes.getValueAt(linha,0))));
            preencheTable(jtbprocessos, "select a.idprocesso as 'Código', a.nome as 'Nome' from processo a inner join  pacote_processo b on a.idprocesso = b.idprocesso where idpacote = "+ pacote.getId() + " and status = 1" );
        }
    };


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                TarefaView dialog = new TarefaView(new javax.swing.JFrame(), true, new AgendaView(new Principal()));
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbcancelar;
    private javax.swing.JButton jbok;
    private javax.swing.JTable jtbpacotes;
    private javax.swing.JTable jtbprocessos;
    private javax.swing.JTextField jtpesquisarpac;
    private javax.swing.JTextField jtpesquisarproc;
    // End of variables declaration//GEN-END:variables
    AgendaView agenda;
    String desc = "";
}
